package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Notice;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INoticeService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    private INoticeService noticeService;

    /**
     * 查询所有通知,并且分页
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Notice> pageInfo = noticeService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/notice/list";
    }


    /**
     * 添加或修改通知
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice) {
        JsonResult result = new JsonResult();
        try {
            noticeService.saveOrUpdate(notice);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }
}
